package com.scau.service.Impl;


import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.scau.dao.Impl.HomeworkDaoImpl;
import com.scau.pojo.Homework;
import com.scau.service.HomeworkService;
@Service
public class HomeworkServiceImpl implements HomeworkService{
	@Autowired
	HomeworkDaoImpl homeworkDAO;

	@Override
	public List<Homework> getHomeworkBySeId(int seId) {
		return homeworkDAO.getHomeworkBySeId(seId);
	}

	@Override
	public int add(JSONObject jsonObject) {
		Homework homework=new Homework();
		String hName=jsonObject.getString("name");
		int seId=jsonObject.getInteger("seId");
		homework.sethName(hName);
		homework.setSeId(seId);
		homework.sethModifytime(new Date());
		homework.sethReleasetime(new Date());
		return homeworkDAO.add(homework);
	}

	@Override
	public int delete(JSONArray jsonArray){
		int result=0;
		List<Integer> idArr=JSON.parseArray(jsonArray.toString(), Integer.class);
		try {
			for(int id : idArr){
				if(homeworkDAO.delete(id)!=1){
					break;
					
				}else{
					result=1;
				}
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return result;
	}

	@Override
	public int update(JSONObject jsonObject) {
		int result=0;
		Homework homework=new Homework();
		int hId=jsonObject.getInteger("id");
		String hName=jsonObject.getString("name");
		int seId=jsonObject.getInteger("seId");
		homework.sethName(hName);
		homework.setSeId(seId);
		homework.sethId(hId);
		homework.sethModifytime(new Date());
		try {
			result=homeworkDAO.update(homework);
			
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return result;
	}

	@Override
	public int getHomeworkCount(int seId) {
		return homeworkDAO.countBySeId(seId);
	}

	@Override
	public List<Homework> queryAllHomework() {
		return homeworkDAO.queryAllHomework();
	}

	@Override
	public int isNameExist(String hName, Integer seId) {
		return homeworkDAO.isNameExist(hName, seId);
	}
}
